<?php
/**
 * 统计每日各种彩种的投注金额与营收状态
 */
defined('IN_YuLin') || exit('NO PERMIT!');
// 获取昨天所有订单， 并按用户计算支出与营收数据， 存入数据库。
echo '<pre>';
// 昨天凌晨的时间点
$btime        = strtotime( date('Y-m-d', strtotime('-1 day')) );
// 统计数据记录表
$statisticTab = Table('statistic_lottery');
// 检查昨天的数据有没有统计到数据库
$checkSql  = 'SELECT count(id) as cnt FROM ' . $statisticTab . ' WHERE datetime = ' . $btime;

$checkFlag = $db->getrow($checkSql);
if ($checkFlag['cnt'] > 0) exit('已统计昨天数据！');

// 今天凌晨的时间点
$etime   = strtotime( date('Y-m-d') );
$mainTab = Table('lottery_order_main');
$itemTab = Table('lottery_order_item');
$where   = ' WHERE createtime >= ' . $btime . ' AND createtime < ' . $etime;
$sql     = 'SELECT item.`id`, item.`o_id`, item.`num`, 
            item.`prize`, main.`amount`, main.`lott_id`, main.`id` as main_id
            FROM ' . $mainTab . ' AS main
            LEFT JOIN ' . $itemTab . ' AS item
            ON main.`id` = item.`o_id`
            ' . $where . '
            ORDER BY main.id DESC';
$todayOrderList = $db->getall($sql);

$checkRepeat   = array();
// 直接按用户计算盈利数据
$statisticData = array();
foreach ($todayOrderList as $k => $v){
    if(!in_array($v['main_id'], $checkRepeat)){
        $statisticData[$v['lott_id']]['bet_amount']           += $v['amount'];
        $statisticData[$v['lott_id']]['prize_amount']         += $v['prize'];
        $statisticData[$v['lott_id']]['winner_amount']        += $v['prize'] - $v['amount'];
        array_push($checkRepeat, $v['main_id']);
    }
    $statisticData[$v['lott_id']]['bet_count']++;
    $statisticData[$v['lott_id']]['order'][$v['o_id']]['bet_nums'] += $v['num'];
    $statisticData[$v['lott_id']]['order'][$v['o_id']]['amount']    = $v['amount'];
}

foreach ($statisticData as $sk => $sv){
    $tmp = array_column($sv['order'], 'bet_nums');
    sort($tmp);
    $statisticData[$sk]['betNumsData']   = $tmp;
    $tmp = array_column($sv['order'], 'amount');
    sort($tmp);
    $statisticData[$sk]['betMoneyData']  = $tmp;
    unset($statisticData[$sk]['order']);
}

foreach ($statisticData as $key => $value){
    $bet_max_nums  = $value['betNumsData'][count($value['betNumsData'])-1];
    $bet_min_nums  = $value['betNumsData'][0];
    $bet_max_money = $value['betMoneyData'][count($value['betMoneyData'])-1];
    $bet_min_money = $value['betMoneyData'][0];
    $ins = array(
        'lott_id'          => $key,
        'bet_amount'       => $value['bet_amount'],
        'prize_amount'     => $value['prize_amount'],
        'winner_amount'    => $value['winner_amount'],
        'bet_count'        => $value['bet_count'],
        'bet_max_nums'     => $bet_max_nums ? $bet_max_nums : 0, // 获取最后一个数据（升序后的数据）
        'bet_min_nums'     => $bet_min_nums ? $bet_min_nums : 0,
        'bet_max_money'    => $bet_max_money ? $bet_max_money : '0.00',
        'bet_min_money'    => $bet_min_money ? $bet_min_money : '0.00',
        'datetime'         => $btime,
    );
    $insSql = 'INSERT INTO ' . $statisticTab . ' SET ' . CreateUpdateSql($ins);
    $db->exec($insSql);
}
echo 'end';

